#include "LSfeatureObject.h"

LSfeatureObject::LSfeatureObject()
{

}
LSfeatureObject::LSfeatureObject(vector<objectPoint> p, int sIdx, int eIdx, int idd)
{
     Scan = p;
    _startIndex = sIdx;
    _endIndex   = eIdx;
    id = idd;
}
void LSfeatureObject::reset()
{
     id          = -1;
    _startIndex  = -1;
    _endIndex    = -1;
}
int LSfeatureObject::length()
{
    Scan.size();
}
LSfeatureObject * LSfeatureObject::CopyTo()
{
    return ( new LSfeatureObject( Scan, _startIndex, _endIndex, id) );
}

int  LSfeatureObject::getPointCount()
{
    return _endIndex - _startIndex + 1;
}

int LSfeatureObject::getMiddleIndex()
{
    return (_startIndex + _endIndex) / 2;
}

objectPoint LSfeatureObject::getMiddlePoint()
{
    return Scan[ getMiddleIndex() ];
}

objectPoint   LSfeatureObject::getStartPoint()
{
    return Scan[ _startIndex ];
}

objectPoint  LSfeatureObject::getEndPoint()
{
    return Scan[ _endIndex ];
}

eConvexHull LSfeatureObject::getBoundingBox()
{
    float Xmin,Xmax,Ymin,Ymax,Rmin,Rmax;
    eConvexHull convex;
    objectPoint p;
    p = Scan.at(_startIndex);
    Xmin = p.getX();
    Ymin = p.getY();
    Xmax = p.getX();
    Ymax = p.getY();

    for(int i = _startIndex + 1; i <= _endIndex; i++)
    {
        p = Scan.at(i);
        Xmin = MinValue(p.getX() ,Xmin);
        Ymin = MinValue(p.getY() ,Ymin);
        Xmax = MaxValue(p.getX() ,Xmax);
        Ymax = MaxValue(p.getY() ,Ymax);
    }
    convex.setMinPoint(Xmin, Ymin);
    convex.setMaxPoint(Xmax,Ymax);
    return convex;
}

objectPoint LSfeatureObject::getMaxPoint()
{
    objectPoint p,pmax;
    pmax = Scan.at(_startIndex);

    for(int i = _startIndex + 1; i <= _endIndex; i++)
    {
        p = Scan.at(i);
        if(p.getX()<pmax.getX())
            pmax = p;
    }
    return pmax;
}

float LSfeatureObject::getDigonalBoundingBox()
{
   eConvexHull bb = getBoundingBox();
    diagonal = fabs(bb.minPoint.getDistanceFromPoint(bb.maxPoint));
    return diagonal;
}

double LSfeatureObject::getLength()
{
  //  cout<<"\n End X: "<<getEndPoint().getX()<<" Y: "<<getEndPoint().getY()<<" start X: "<<getStartPoint().getX()<<" Y: "<<getStartPoint().getY()<<" dist "<<getStartPoint().getDistanceFromPoint( getEndPoint() )<<" Sin "<<getStartPoint().Index<<" Ein "<<getEndPoint().Index;
    return getStartPoint().getDistanceFromPoint( getEndPoint() );
}

const LSfeatureObject & LSfeatureObject::operator +=( LSfeatureObject B)
{
    //cout<<"\n Id: "<<id<<" B.id: "<<B.id;

    return   LSfeatureObject(Scan,
                            min(_startIndex , B._startIndex),
                            max(_endIndex   , B._endIndex),
                            max(id , B.id) );
}

double LSfeatureObject::getWidth()
{
    return distancePointLine( getMaxPoint() , Scan[_startIndex] , Scan[_endIndex]);
}
